﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace ProjetS5
{
    public class Box
    {
        public static GraphicsDevice device;
        public Vector2 position;
        public Vector2 dimension;
        public String contentText;
        private bool showRectangle;
        private Color boxColor;
        private Color textColor;
        private SpriteFont spriteFont;
        private int padding = 0;
        private float fontScale;
        private bool isDrawnAsCenter = false;
        private Color altBoxColor;
        private Color altTextColor;
        public bool isHover = false;
        public bool isSelected = false;

        #region for calculation purpose

        private Rectangle textDimension;
        private Rectangle boxDimension;
        private Texture2D box;
        private Texture2D altBox;

        #endregion

        public Box(Vector2 Position, Vector2 Dimension, Color BoxColor, Color AltBoxColor)
            : this(Position, Dimension, BoxColor, AltBoxColor, "", Color.Transparent, Color.Transparent, true, null, 1) { }
        public Box(Vector2 Position, String ContentText, Color TextColor, Color AltTextColor, SpriteFont SpriteFont, float FontScale)
            : this(Position, new Vector2(-1), Color.Transparent, Color.Transparent, ContentText, TextColor, AltTextColor, false, SpriteFont, FontScale) { }
        public Box(Vector2 Position, String ContentText, Color TextColor, Color AltTextColor, bool ShowRectangle, SpriteFont SpriteFont, float FontScale, int Padding)
            : this(Position, new Vector2(-1), Color.Transparent, Color.Transparent, ContentText, TextColor, AltTextColor, ShowRectangle, SpriteFont, FontScale)
        {
            padding = Padding;
            boxDimension = new Rectangle(0, 0, textDimension.Width + padding, textDimension.Height + padding);
        }
        public Box(Vector2 Position, Vector2 Dimension, Color BoxColor, Color AltBoxColor, String ContentText, Color TextColor, Color AltTextColor, bool ShowRectangle, SpriteFont SpriteFont, float FontScale)
        {
            position = Position;
            dimension = Dimension;
            boxColor = BoxColor;
            contentText = ContentText;
            textColor = TextColor;
            showRectangle = ShowRectangle;
            spriteFont = SpriteFont;
            fontScale = FontScale;
            altBoxColor = AltBoxColor;
            altTextColor = AltTextColor;

            if (spriteFont != null)
            {
                Vector2 stringDimension = spriteFont.MeasureString(contentText);
                textDimension = new Rectangle(0, 0, (int)stringDimension.X, (int)stringDimension.Y);  
            }
            UpdateBox();
        }
        public void UpdateBox()
        {
            if (dimension.X != -1 && dimension.Y != -1)
            {
                boxDimension = new Rectangle(0, 0, (int)dimension.X + padding * 2, (int)dimension.Y + padding * 2);
                box = new Texture2D(device, boxDimension.Width, boxDimension.Height);
                altBox = new Texture2D(device, boxDimension.Width, boxDimension.Height);

                Color[] color = new Color[box.Width * box.Height];
                Color[] altColor = new Color[box.Width * box.Height];

                for (int i = 0; i < box.Width * box.Height; i++)
                {
                    color[i] = boxColor;
                    altColor[i] = altBoxColor;
                }

                box.SetData<Color>(color);
                altBox.SetData<Color>(altColor);
            }
        }

        public bool IsThisInBox(Vector2 Point)
        {
            bool toReturn = false;
            if (dimension.X != -1 && dimension.Y != -1)
            {
                toReturn = (Point.X < (position.X + dimension.X + padding - (isDrawnAsCenter ? (1.0 / 2.0) * dimension.X : 0)) && Point.X >= (position.X - padding - (isDrawnAsCenter ? (1.0 / 2.0) * dimension.X : 0)) && Point.Y < (position.Y + dimension.Y + padding - (isDrawnAsCenter ? (1.0 / 2.0) * dimension.Y : 0)) && Point.Y >= (position.Y - padding - (isDrawnAsCenter ? (1.0 / 2.0) * dimension.Y : 0)));
            }
            else
            {
                Vector2 stringDimension = spriteFont.MeasureString(contentText);
                toReturn = (Point.X < (position.X + stringDimension.X + padding - (isDrawnAsCenter ? (1.0 / 2.0) * stringDimension.X : 0)) && Point.X >= (position.X - padding - (isDrawnAsCenter ? (1.0 / 2.0) * stringDimension.X : 0)) && Point.Y < (position.Y + stringDimension.Y + padding - (isDrawnAsCenter ? (1.0 / 2.0) * stringDimension.Y : 0)) && Point.Y >= (position.Y - padding - (isDrawnAsCenter ? (1.0 / 2.0) * stringDimension.Y : 0)));
            }
            isHover = toReturn;
            return toReturn;
        }
        public void DrawAsCenter(SpriteBatch SpriteBatch)
        {
            if (showRectangle)
            {
                SpriteBatch.Draw((isHover || isSelected) ? altBox : box, new Rectangle((int)(position.X - padding - (dimension.X / 2)), (int)(position.Y - padding - (dimension.Y / 2)), (int)(dimension.X + padding * 2), (int)(dimension.Y + padding * 2)), (isHover || isSelected) ? altBoxColor : boxColor);
            }
            if(spriteFont != null)
            {
                Vector2 stringDimension = spriteFont.MeasureString(contentText) * fontScale;
                SpriteBatch.DrawString(spriteFont, contentText, new Vector2(position.X - (stringDimension.X / 2), position.Y - (stringDimension.Y / 2)), (isHover || isSelected) ? altTextColor : textColor, 0, Vector2.Zero, fontScale, SpriteEffects.None, 0);
            }
            isDrawnAsCenter = true;
        }
        public void Draw(SpriteBatch SpriteBatch)
        {
            if (showRectangle)
            {
                SpriteBatch.Draw((isHover || isSelected) ? altBox : box, new Rectangle((int)(position.X - padding), (int)(position.Y - padding), (int)(dimension.X + padding * 2), (int)(dimension.Y + padding * 2)), (isHover || isSelected) ? altBoxColor : boxColor);
            }
            if (spriteFont != null)
            {
                SpriteBatch.DrawString(spriteFont, contentText, position, (isHover || isSelected) ? altTextColor : textColor, 0, Vector2.Zero, fontScale, SpriteEffects.None, 0);
            }
            isDrawnAsCenter = false;
        }
    }
}
